<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增用户端轮播管理')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-userBanner-add">
            <input id="storeId" name="storeId" type="hidden" class="form-control">
            <input id="partsId" name="partsId" type="hidden" class="form-control">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">广告名称：</label>
                <div class="col-sm-8">
                    <input name="bannerName" class="form-control" type="text" autocomplete="off" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">轮播图：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="bannerImageUrl" required>
                    <div class="file-loading">
                        <input class="form-control file-upload" id="bannerImageUrl" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">状态：</label>
                <div class="col-sm-8">
                    <select name="bannerState" class="form-control m-b" th:with="type=${@dict.getType('banner_state')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">分类：</label>
                <div class="col-sm-8">
                    <select name="bannerType" id="bannerType" onchange="stateVulue()" class="form-control m-b" th:with="type=${@dict.getType('banner_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div id="bannerUrlDiv" class="form-group">
                <label class="col-sm-3 control-label">公众号链接：</label>
                <div class="col-sm-8">
                    <textarea name="bannerUrl" id="bannerUrl" class="form-control" type="text" autocomplete="off" rows="5"></textarea>
                </div>
            </div>
            <div id="bannerDetailsDiv" class="form-group">
                <label class="col-sm-3 control-label">图文详情：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="bannerDetails">
                    <div class="summernote" id="bannerDetails"></div>
                </div>
            </div>
            <div id="bannerStoreTypeDiv" class="form-group">
                <label class="col-sm-3 control-label">选择店铺类型：</label>
                <div class="col-sm-8">
                    <select name="bannerStoreType" id="bannerStoreType" class="form-control m-b" th:with="type=${@dict.getType('store_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div id="storeNameDiv" class="form-group">
                <label class="col-sm-3 control-label">选择店铺：</label>
                <div class="col-sm-8">
                    <input id="storeName" name="storeName" class="form-control" type="text" placeholder="请选择店铺" autocomplete="off" onclick="storeSelect()">
                </div>
            </div>
            <div id="partsNameDiv" class="form-group">
                <label class="col-sm-3 control-label">选择产品：</label>
                <div class="col-sm-8">
                    <input id="partsName" name="partsName" class="form-control" type="text" placeholder="请选择产品" autocomplete="off" onclick="partsSelect()">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "zsyh/userBanner"
        $("#form-userBanner-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-userBanner-add').serialize());
            }
        }

        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/uploadOSS',
            maxFileCount: 1,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        }).on("filebatchselected", function(event, files) {
            $(this).fileinput("upload");
        })

        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/uploadOSS",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });

            $("#storeNameDiv").css("display","none");
            $("#partsNameDiv").css("display","none");
            $("#bannerStoreTypeDiv").css("display","none");
            $("#bannerUrlDiv").css("display","none");

        });

        function stateVulue() {
            //获取被选中的option标签
            var vs = $('#bannerType  option:selected').val();
            if(vs == "1"){
                $("#bannerDetailsDiv").show();
                $("#storeNameDiv").hide();
                $("#partsNameDiv").hide();
                $("#bannerStoreTypeDiv").hide();
                $("#bannerUrlDiv").hide();
            }else if(vs == "2"){
                //店铺
                $("#storeNameDiv").show();
                $("#bannerStoreTypeDiv").show();
                $("#bannerDetailsDiv").hide();
                $("#partsNameDiv").hide();
                $("#bannerUrlDiv").hide();
            }else if(vs == "3"){
                $("#partsNameDiv").show();
                $("#bannerDetailsDiv").hide();
                $("#storeNameDiv").hide();
                $("#bannerStoreTypeDiv").hide();
                $("#bannerUrlDiv").hide();
            }else if(vs == "4"){
                $("#partsNameDiv").hide();
                $("#bannerDetailsDiv").hide();
                $("#storeNameDiv").hide();
                $("#bannerStoreTypeDiv").hide();
                $("#bannerUrlDiv").hide();
            }else if(vs == "5"){
                $("#partsNameDiv").hide();
                $("#bannerDetailsDiv").hide();
                $("#storeNameDiv").hide();
                $("#bannerStoreTypeDiv").hide();
                $("#bannerUrlDiv").show();
            }

        }

        function partsSelect() {
            var url = ctx+"zsyh/parts/bannerView/";
            layer.open({
                type: 2,
                area: ['1200px', ($(window).height()-50) + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: "选择产品",
                content:  url,
                closeBtn: 1,
                // 弹层外区域关闭
                shadeClose: true,
                end: function(){
                    //location.reload();
                },
                cancel: function(index) {
                    //location.reload();
                }
            });

        }

        function storeSelect() {
            var url = ctx+"zsyh/repairStore/bannerView/";
            layer.open({
                type: 2,
                area: ['1200px', ($(window).height()-50) + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: "选择店铺",
                content:  url,
                closeBtn: 1,
                // 弹层外区域关闭
                shadeClose: true,
                end: function(){
                    //location.reload();
                },
                cancel: function(index) {
                    //location.reload();
                }
            });

        }

    </script>
</body>
</html>